﻿using UnityEngine;
using System.Collections;
using Prime31;


public class thisController : MonoBehaviour
{
    public float gravity = -25f;
    public float runSpeed = 8f;
    public float groundDamping = 20f; // 改变方向的速度
    public float inAirDamping = 5f;
    public float jumpHeight = 3f;
    public bool IsDoubleJump = false;
    public bool DoubleJumpCheck = true;
    public bool InfiniteJump = false;

    [HideInInspector]
    private float normalizedHorizontalSpeed = 0;

    private CharacterController2D _controller;
    private Animator _animator;
    private RaycastHit2D _lastControllerColliderHit;
    private Vector3 _velocity;


    void Awake()
    {
        _animator = GetComponent<Animator>();
        _controller = GetComponent<CharacterController2D>();

        //监听事件
        _controller.onControllerCollidedEvent += onControllerCollider;
        _controller.onTriggerEnterEvent += onTriggerEnterEvent;
        _controller.onTriggerExitEvent += onTriggerExitEvent;
    }


    #region Event Listeners

    void onControllerCollider(RaycastHit2D hit)
    {
        if (hit.normal.y == 1f)
            return;

        //Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
    }


    void onTriggerEnterEvent(Collider2D col)
    {
        Debug.Log("onTriggerEnterEvent: " + col.gameObject.name);
    }


    void onTriggerExitEvent(Collider2D col)
    {
        Debug.Log("onTriggerExitEvent: " + col.gameObject.name);
    }

    #endregion


    void Update()
    {
        if (_controller.isGrounded)
            _velocity.y = 0;

        if (Input.GetKey(KeyCode.D))
        {
            normalizedHorizontalSpeed = 1;
            if (transform.localScale.x < 0f)
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);


            if (_controller.isGrounded)
            {
                if (Input.GetKey(KeyCode.J))
                {
                    _animator.Play(Animator.StringToHash("player_runshoot"));
                }
                else
                    _animator.Play(Animator.StringToHash("player_run"));                
            }

        }
        else if (Input.GetKey(KeyCode.A))
        {
            normalizedHorizontalSpeed = -1;
            if (transform.localScale.x > 0f)
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);

            if (_controller.isGrounded)
            {
                if (Input.GetKey(KeyCode.J))
                {
                    _animator.Play(Animator.StringToHash("player_runshoot"));
                }
                else
                    _animator.Play(Animator.StringToHash("player_run"));
            }
        }
        else
        {
            normalizedHorizontalSpeed = 0;

            if (_controller.isGrounded)
            {
                _animator.Play(Animator.StringToHash("player_stand"));  
            }
         
        }


        // 只能在落地时起跳
        if (_controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
        {
            _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
            _animator.Play(Animator.StringToHash("player_jump"));
            AudioController.Play("playerjump01");
        }

        if (((!_controller.isGrounded && IsDoubleJump && DoubleJumpCheck)||InfiniteJump) && Input.GetKeyDown(KeyCode.Space))
        {
            _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
            _animator.Play(Animator.StringToHash("player_jump"));
            AudioController.Play("playerjump02");
            DoubleJumpCheck = false;
        }

        if (_controller.isGrounded)
        {
            DoubleJumpCheck = true;
        }

        var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping;
        _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

        // 在运动前应用重力
        _velocity.y += gravity * Time.deltaTime;

        // 如果按S，增加移动量且关闭单向平台的检测帧
        // 可以通过单向平台
        if (_controller.isGrounded && Input.GetKey(KeyCode.S))
        {
            _velocity.y *= 3f;
            _controller.ignoreOneWayPlatformsThisFrame = true;
        }

        _controller.move(_velocity * Time.deltaTime);

        // 获取当前_velocity作为所有计算基础
        _velocity = _controller.velocity;
    }

}
